#region File Description
//-----------------------------------------------------------------------------
// SpriteFontControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

#region Using System to need Animation
using System.Diagnostics;
using System.Windows.Forms;
#endregion

#region Aim Engine Using
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

using AimGameEngine.NodeEditorObject;
using AimGameEngine.EditorObject;

using Microsoft.Xna.Framework.Input;

using AimGameLibrary;
using AimGameLibrary.GameObject;
using AimGameLibrary.ScreenManager;

#endregion

namespace AimGameEngine.Compornet
{
    /// <summary>
    /// Example control inherits from GraphicsDeviceControl, which allows it to
    /// render using a GraphicsDevice. This control shows how to use ContentManager
    /// inside a WinForms application. It loads a SpriteFont object through the
    /// ContentManager, then uses a SpriteBatch to draw text. The control is not
    /// animated, so it only redraws itself in response to WinForms paint messages.
    /// </summary>
    class SpritePreviewControl : GraphicsDeviceControl
    {
        ContentManager content;
        SpriteBatch spriteBatch;
        SpriteFont font;

        Stopwatch timer;

        AimGameControl aimgamecontrol;
        public AimGameControl AimgamecontroL { get { return aimgamecontrol; } }

        Grid grid;
        //float TempElapeTime;
        //static float PreviewTime;
        /// <summary>
        /// Initializes the control, creating the ContentManager
        /// and using it to load a SpriteFont.
        /// </summary>
        protected override void Initialize()
        {
            content = new ContentManager(Services, "Content");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = content.Load<SpriteFont>("Arial");

            // Start the animation timer.
            timer = Stopwatch.StartNew();
            //TempElapeTime = 0;
            // Hook the idle event to constantly redraw our animation.
            Application.Idle += delegate { Invalidate(); };

            aimgamecontrol = new AimGameControl();
            
            grid = new Grid(spriteBatch, aimgamecontrol.CamerA, content.Load<Texture2D>("Node/pixel"));
           
            //BG = content.Load<Texture2D>("ForPresent/Test_bg");
            //Player = content.Load<Texture2D>("ForPresent/Suthon");
        }


        /// <summary>
        /// Disposes the control, unloading the ContentManager.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                content.Unload();
            }

            base.Dispose(disposing);
        }


        /// <summary>
        /// Draws the control, using SpriteBatch and SpriteFont.
        /// </summary>
        /// 
        //float Test;
        //int current = 0;
        protected override void Draw()
        {
            // Spin the triangle according to how much time has passed.
            //float time = (float)timer.Elapsed.TotalSeconds;
            aimgamecontrol.UpdateTime(timer.Elapsed);

            grid.myArea = this.ClientRectangle;
            grid.UpdateTime(timer.Elapsed);

            String message = "This is Preview ";
            //TempElapeTime = (float)timer.Elapsed.TotalMilliseconds;
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            grid.Draw();
            spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.NonPremultiplied);

            spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.00001f);

            aimgamecontrol.Draw();

            spriteBatch.End();

            
        }

        public void Refresh(ContentManager contentmanager ,Script create)
        {
            aimgamecontrol.UnLoadContent();

            /// Clean Content when bug Load content.
            String Root = contentmanager.RootDirectory;
            contentmanager = new ContentManager(this.Services, Root);

            create.LoadContent(contentmanager, this.spriteBatch);
            create.RunLoadObject(null, aimgamecontrol);
            //create.RunLoadObject(null,this.DictionaryGameObject
            //   , this.ListUpdateTime, this.ListDraw,this.ListHandle);
        }

    }
}
